﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.BLL;
using Shanfree.SharpMovie.Model;
using System.Web.UI.WebControls;

public partial class List_Mine : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string movieID = GetRequest("movieID");
            MovieInfo nInfo = new Movie().GetByID(movieID);
            tbMovie.Text = nInfo.EnglishName;
            BindList(pGrid.CurrentPageIndex);
        }
    }

    #region 列表控件事件
    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void pGrid_OnClick(object sender, EventArgs e)
    {
        try
        {
            BindList(pGrid.CurrentPageIndex);
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    private void BindList(int p)
    {
        IList<ListInfo> list = new List().GetList(this.CurrentUser.ID, gvList.PageSize, pGrid.CurrentPageIndex);
        gvList.DataSource = list;
        gvList.DataBind();
        CustomList<ListInfo> cList = list as CustomList<ListInfo>;
        if (null != cList)
        {
            pGrid.PageSize = gvList.PageSize;
            pGrid.TotalAmout = cList.TotalAmout;
        }
    }
    #endregion

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            string movieID = GetRequest("movieID");
            foreach (GridViewRow row in gvList.Rows)
            {
                CheckBox cbSel = row.FindControl("cbSel") as CheckBox;
                if (cbSel.Checked == true)
                {
                    string listID = cbSel.Attributes["value"];
                    string remark = tbListRemark.Text;
                    new Shanfree.SharpMovie.BLL.ListItem().Add(listID,movieID, remark);
                }
            }
            ShowMsg("影片收藏成功。");
        }
        catch (Exception ex)
        {
            ShowMsg("生成出错：" + ex.Message);
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbSel = e.Row.FindControl("cbSel") as CheckBox;
                MovieInfo nInfo = e.Row.DataItem as MovieInfo;
                cbSel.Attributes["value"] = nInfo.ID;
            }
        }
        catch (Exception ex)
        {
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }
    protected void btnALLList_Click(object sender, EventArgs e)
    {
        string movieID = GetRequest("movieID");
        string url = string.Format("../List/Add.aspx?movieid={0}", movieID);
        Response.Redirect(url, false);
    }
}
